/*
 * physics.hpp
 *
 *  Created on: 30.03.2011
 *      Author: inkooboo
 */

#ifndef _SYSTEMS_PHYSICS_HPP_
# define _SYSTEMS_PHYSICS_HPP_

# include "system.hpp"
# include "core/object.hpp"
# include "core/motion_state.hpp"

# include <btBulletDynamicsCommon.h>

# include <boost/noncopyable.hpp>
# include <boost/shared_ptr.hpp>
# include <boost/cstdint.hpp>
# include <boost/thread.hpp>

# include <map>

class physics_manager_t : private boost::noncopyable, public if_system_t
{
    struct simulation_object_t
    {
        boost::shared_ptr<motion_state_t> motion_state;
        boost::shared_ptr<btCollisionShape> shape;
        boost::shared_ptr<btRigidBody> rigid_body;
    };

    typedef std::map<boost::uint64_t, boost::shared_ptr<simulation_object_t> > sim_obj_map_t;
public:

    static boost::shared_ptr<if_system_t> create(f_engine_t *engine);

    void start(); /*override*/

    void stop();

    void add_simulation_object(boost::uint64_t ID, boost::shared_ptr<motion_state_t> motion_state, vector3_t &bounding_box, scalar_t mass);
    void remove_simulation_object(boost::uint64_t ID);

    ~physics_manager_t();

private:
    physics_manager_t(f_engine_t *engine);

    sim_obj_map_t sim_obj_map_;

    void simulation_thr();

    boost::thread simulation_thr_;

    boost::shared_ptr<btBroadphaseInterface> broadphase_;
    boost::shared_ptr<btDefaultCollisionConfiguration> collision_configuration_;
    boost::shared_ptr<btCollisionDispatcher> dispatcher_;
    boost::shared_ptr<btSequentialImpulseConstraintSolver> solver_;
    boost::shared_ptr<btDiscreteDynamicsWorld> dynamics_world_;


    //temporary place ground physics model there
    boost::shared_ptr<btCollisionShape> ground_shape_;
    boost::shared_ptr<btDefaultMotionState> ground_motion_state_;
    boost::shared_ptr<btRigidBody> ground_rigid_body_;

    f_engine_t *engine_;
};


#endif /* _SYSTEMS_PHYSICS_HPP_ */
